using System;
using System.Linq;
using System.Reflection;
using MetaObjects.Contracts;

namespace MetaObjects.Core
{
	public class TypeProvider : MetaEntityProvider<Type>
	{
		public IMetaFieldProvider FieldProvider { get; private set; }

		public TypeProvider() : this(new PropertyInfoProvider())
		{
		}

		public TypeProvider(IMetaFieldProvider fieldProvider)
		{
			FieldProvider = fieldProvider;
		}

		protected override IMetaEntity GetMetaEntity(Type entity)
		{
			var fields = entity.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Select(FieldProvider.FieldFactory);
			MetaEntity<object> metaEntity = new MetaEntity<object>(entity.Name, () => Activator.CreateInstance(entity), fields);
			return metaEntity;
		}
	}
}